<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import userStore from '../store/modules/user';
import { registerApi } from '../api/user';
// 导入Element Plus组件
import { ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus';
import 'element-plus/dist/index.css';

const xianshi = ref(true);
const router = useRouter()
const user = userStore();
const Denglu = reactive({
    username: '',
    password: ''
})
// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ]
};
// 注册表单验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少为 6 个字符', trigger: 'blur' }
  ]
};
// 表单引用
const loginFormRef = ref();
const registerFormRef = ref();
const handleLogin = async () => {
    // 验证表单
    loginFormRef.value.validate(async (valid) => {
        if (valid) {
            try {
                let result = await user.loginuser(Denglu)
                if (result === true) {
                    ElMessage.success('登录成功');
                    router.push('/home')
                } else {
                    ElMessage.error('登录失败，请检查用户名和密码');
                }
            } catch (error) {
                console.error('登录异常:', error);
                ElMessage.error('登录异常，请稍后重试');
            }
        }
    });
}
const fan = () => {
    // 切换表单显示状态
    xianshi.value = !xianshi.value;
    // 清空表单内容
    Denglu.username = '';
    Denglu.password = '';
    // 重置表单验证状态
    if (loginFormRef.value) {
      loginFormRef.value.resetFields();
    }
    if (registerFormRef.value) {
      registerFormRef.value.resetFields();
    }
}
const register = async () => {
    // 验证表单
    registerFormRef.value.validate(async (valid: boolean) => {
        if (valid) {
            try {
                let result = await registerApi(Denglu)
                if (result.code === 200) {
                    ElMessage.success('注册成功');
                    fan();
                } else {
                    ElMessage.error('注册失败：' + (result.msg || '未知错误'));
                }
            } catch (error) {
                console.error('注册异常:', error);
                ElMessage.error('注册异常，请稍后重试');
            }
        }
    });
}
</script>

<template>
  <div class="login-container" v-if="xianshi">
    <div class="login-card">
      <div class="login-header">用户登录</div>
      <el-form ref="loginFormRef" :model="Denglu" :rules="loginRules" label-width="0px">
        <el-form-item prop="username">
          <el-input 
            v-model="Denglu.username" 
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="Denglu.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleLogin"
            style="width: 100%"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <span class="switch-btn" @click="fan">注册 -></span>
    </div>
  </div>
  <div class="login-container" v-else>
    <div class="login-card">
      <div class="login-header">用户注册</div>
      <el-form ref="registerFormRef" :model="Denglu" :rules="registerRules" label-width="0px">
        <el-form-item prop="username">
          <el-input 
            v-model="Denglu.username" 
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="Denglu.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            @click="register"
            style="width: 100%"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <span class="switch-btn" @click="fan">登录-></span>
    </div>
  </div>
</template>

<style scoped>
@use '@styles/index.scss';
.login-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    position: relative;
    overflow: hidden;
    .login-card {
        position: relative;
        z-index: 1;
        background: #ffffff;
        backdrop-filter: blur(10px);
        border-radius: 12px;
        padding: 40px 30px;
        box-shadow:
            0 8px 24px rgba(0, 0, 0, 0.1),
            0 4px 12px rgba(0, 0, 0, 0.05),
            inset 0 1px 0 rgba(255, 255, 255, 0.8);
        border: 1px solid #e0e0e0;
        max-width: 400px;
        width: 90%;
        animation: cardEntrance 0.6s ease-out;
        .login-header {
            text-align: center;
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 30px;
            color: #333333;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            position: relative;
        }
        /* Element Plus表单样式调整 */
        :deep(.el-form) {
            width: 100%;
        }
        :deep(.el-form-item) {
            margin-bottom: 24px;
        }
        :deep(.el-input__wrapper) {
            border-radius: 8px;
            transition: all 0.3s ease;
        }
        :deep(.el-input__wrapper.is-focus) {
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            border-color: #409eff;
        }
        :deep(.el-input__inner) {
            font-size: 16px;
            padding: 12px 16px;
            height: 44px;
        }
        :deep(.el-button) {
            padding: 12px 20px;
            font-size: 16px;
            font-weight: 600;
            border-radius: 8px;
            transition: all 0.3s ease;
        }
        :deep(.el-button:hover) {
            transform: translateY(-2px);
            box-shadow: 0 6px 18px rgba(64, 158, 255, 0.3);
        }
        :deep(.el-button:active) {
            transform: translateY(-1px);
        }
    }
}

.login-container::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background:
        radial-gradient(circle at 20% 20%, rgba(0, 0, 0, 0.03) 0%, transparent 50%),
        radial-gradient(circle at 80% 80%, rgba(0, 0, 0, 0.02) 0%, transparent 50%);
    z-index: 0;
}

@keyframes cardEntrance {
    0% {
        opacity: 0;
        transform: translateY(30px) scale(0.95);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.login-header::after {
    content: '';
    position: absolute;
    bottom: -8px;
    left: 25%;
    width: 50%;
    height: 2px;
    background: #333333;
    border-radius: 2px;
}

/* 切换按钮样式 */
.switch-btn {
    display: block;
    text-align: center;
    margin-top: 15px;
    color: #606266;
    cursor: pointer;
    transition: color 0.3s;
    font-size: 14px;
}

.switch-btn:hover {
    color: #409eff;
}

/* 响应式设计 */
@media (max-width: 480px) {
    .login-card {
        padding: 30px 20px;
        margin: 15px;
    }
    .login-header {
        font-size: 24px !important;
        margin-bottom: 25px !important;
    }
    .login-card :deep(.el-input__inner) {
        font-size: 14px;
        padding: 10px 14px;
        height: 40px;
    }
    .login-card :deep(.el-button) {
        padding: 10px 18px;
        font-size: 14px;
    }
}

/* 添加额外的视觉元素以增强精美感 */
.login-card::before {
    content: '';
    position: absolute;
    top: -2px;
    left: -2px;
    right: -2px;
    bottom: -2px;
    background: linear-gradient(45deg, #cccccc, #999999, #666666, #cccccc);
    border-radius: 14px;
    z-index: -1;
    opacity: 0.2;
    filter: blur(8px);
}
</style>